#!/usr/bin/python
# -*- coding: utf-8 -*-

from mc102html import document, div, h1, h2, h3, h4, p, \
     link, text, table, tr, th, td, ul, li, span
from os import chmod

class index:

    def __init__(self):
        self.TERM="none"
        self.GROUPS_NAME = "none"
        self.DISCIPLINE_CODE = "MC999"
        self.DISCIPLINE_NAME = "Disciplina Inválida"        
        self.SCORED_TESTS = []
        self.BASEDIR=".."
        self.HTMLDIR="html"
        self.CLASSES = {}
        self.CLASSESDIR = "aulas"
        self.CLASS_GENERATOR = "Makefile"
        self.CLASS_GENERATOR_COMMAND = "make"
        self.CLASSPDF = ""
        self.CLASSPDF4UP = ""
        self.CLASSEXTRAS = ""
        self.CLASSFILENAME = ""
        self.LABELS = {}
        self.MODE = 0
        self.SUSY_URL = ""
        self.NEWS = []

    def model(self):
        # Cria a página
        page = document("MC102 - Algoritmos e programação de computadores","",
                        div ("titulo",
                             h1("%s - %s" % (self.DISCIPLINE_CODE,
                                             self.DISCIPLINE_NAME)),
                             h4("%s, Turmas %s" % (self.TERM,
                                                   self.GROUPS_NAME)),
                             p(link("http://www.ic.unicamp.br", text("Instituto de computação")),
                               text(" - "),
                               link("http://www.unicamp.br", text("UNICAMP"))
                               )
                             )    
                        )
        return page

    def generate_index_html (self):

        pattern = {}

        #Gerando strings que podem ser usadas na pagina principal
        susy = link("%s" % (self.SUSY_URL), text("Susy"))
        pattern["susy"] = susy.tohtml()

        scores = link("scores/index.html", text("notas"))        
        pattern["scores"] = scores.tohtml()

        source = open("index.html.model", "r")
        txt = source.read()

        # Gerando notícias
        news = ul()
        self.NEWS.sort()
        self.NEWS.reverse()
        for i in self.NEWS:
            news.add(li(span("data", text("%02d/%02d" % (i[1],i[0]))),
                        span("topico", text("%s: " % (i[2]))),
                        text(i[3] % pattern)))

        # Gerando aulas
        classes = ul()

        class_list = self.CLASSES.keys()
        class_list.sort()

        i = 1
        for cl in class_list:
            first = True
            line = li(text("Aula %02d: %s" % (i, self.CLASSES[cl])))
            if self.CLASSPDF != "":
                if first:
                    line.add(text(" ("))
                else:
                    line.add(text(","))
                line.add(link("class%s/%s%s.pdf" %
                              (cl, self.CLASSFILENAME, cl),
                              text("slides")))
                first = False

            if self.CLASSPDF4UP != "":
                if first:
                    line.add(text(" ("))
                else:
                    line.add(text(", "))
                line.add(link("class%s/%s%s-4up.pdf" %
                              (cl, self.CLASSFILENAME, cl),
                              text("slides para impressão")))
                first = False

            if self.CLASSEXTRAS != "":
                if first:
                    line.add(text(" ("))
                else:
                    line.add(text(", "))
                line.add(link("class%s/" %
                              (cl),
                              text("exemplos")))
                first = False
                
            if not first:
                line.add(text(")"))
            i = i + 1
            classes.add(line)

        pattern["news"] = news.tohtml()
        pattern["classes"] = classes.tohtml()

        txt = txt % pattern

        page = self.model()
        page.add(text(txt))
        
        arq = open("%s/index.html" % (self.HTMLDIR), "w")
        arq.write(page.tohtml())
        arq.close()

        chmod("%s/index.html" % (self.HTMLDIR), self.MODE)
            
    def generate_windows_html (self):
        source = open("windows.html.model", "r")
        txt = source.read()

        page = self.model()
        page.add(text(txt))

        arq = open("%s/windows.html" % (self.HTMLDIR), "w")
        arq.write(page.tohtml())
        arq.close()

        chmod("%s/windows.html" % (self.HTMLDIR), self.MODE)
        
